package com.programs.waw;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.text.InputFilter;
import android.text.InputFilter.LengthFilter;
import android.text.Spanned;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class CustomDialog extends Dialog implements
		android.view.View.OnKeyListener, android.view.View.OnClickListener {

	public interface ReadyListener {
		public void ready(String s);
	}

	private String name;
	private int number;
	private Boolean aliceFlag;
	private ReadyListener readyListener;
	private Handler handler;
	private Runnable r;

	private EditText editText;
	private Button okButton;
	private Button cancelButton;

	public CustomDialog(Context context, String name, int number,
			Boolean aliceFlag, Handler handler, Runnable r,
			ReadyListener readyListener) {
		super(context);
		this.name = name;
		this.number = number;
		this.aliceFlag = aliceFlag;
		this.handler = handler;
		this.r = r;
		this.readyListener = readyListener;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);

		setContentView(R.layout.custom_dialog);

		handler.removeCallbacks(r);

		setTitle("Inserimento Manuale");

		okButton = (Button) findViewById(R.id.okButton);
		okButton.setOnClickListener(new buttonListener());

		cancelButton = (Button) findViewById(R.id.cancelButton);
		cancelButton.setOnClickListener(this);

		TextView nameTextView = (TextView) findViewById(R.id.nameTextView);
		nameTextView.setText(name);

		editText = (EditText) findViewById(R.id.ssidEditText);
		editText.setOnKeyListener(this);

		InputFilter filter = new InputFilter() {
			@Override
			public CharSequence filter(CharSequence source, int start, int end,
					Spanned dest, int dstart, int dend) {

				if (aliceFlag) {
					for (int i = start; i < end; i++) {
						if (!Character.isDigit(source.charAt(i))) {
							return "";
						}
					}
				} else {
					for (int i = start; i < end; i++) {
						if ((!Character.isDigit(source.charAt(i)) && (source
								.charAt(i) < 'a' || source.charAt(i) > 'f'))) {
							return "";
						}
					}
				}

				return null;
			}
		};

		InputFilter[] filters = new InputFilter[] { filter,
				new LengthFilter(number) };
		editText.setFilters(filters);

	}

	@Override
	public boolean onKey(View v, int keyCode, KeyEvent event) {

		Log.i("Key press", "" + editText.getText().length());

		if (editText.getText().length() < number) {
			okButton.setEnabled(false);
		} else {
			okButton.setEnabled(true);
		}

		return false;
	}

	@Override
	public void onClick(View v) {
		handler.post(r);
		this.dismiss();
	}

	private class buttonListener implements android.view.View.OnClickListener {
		@Override
		public void onClick(View v) {
			readyListener.ready(name + String.valueOf(editText.getText()));
			CustomDialog.this.dismiss();
		}
	}

}
